# coding=utf-8
import os
import sys
from xml.etree import ElementTree

from OpenUmeaRecreationalFacilities import *

""" Because ckanclient won't work with python 3 yet, download this manually from:
	$ wget http://openumea.se/dataset/recreational-facilities

	API documentation for ckanclient is otherwise available at development site:
	https://github.com/okfn/ckanclient

	Make sure the filename matches the variable below and is in your current folder.
"""
FILE = 'Facilities.xml'

def print_err(*args):
	print(*args, file=sys.stderr)
	pass

print_err("parsing XML")
full_tree = ElementTree.parse(FILE)
xml_root  = full_tree.getroot()

indexdir = '{0}/facilities/'.format(os.getcwd())
indexxml = indexdir + '/facilities.xml'
os.makedirs(indexdir, exist_ok=True)
markerout = open(indexdir + '/markers.tsv', mode='w')
markercols = '{0}\t{1}\t{2}\t{3}\t{4}\n'.format('id', 'lat', 'lon', 'title', 'description')
markerout.write(markercols)

print_err("traversing XML")
for element in list(xml_root):
	try:
		if int(element.attrib['ID']) <= 0:	# use .get('ID', 0) instead?
			continue
			#raise ValueError("Invalid Facility ID: {0}".format(element.attrib['ID']))

		current = Facility(element)
		if current.Coordinate == None:
			continue

		filedir = indexdir + '{0}/'.format(current.ID)
		xmlfile = filedir + 'object.xml'
		objfile = filedir + 'facility.tsv'
		geofile = filedir + 'coords.txt'
		imgdir  = filedir + 'images'

		os.makedirs(imgdir, exist_ok=True)

		with open(xmlfile, mode='w', encoding='utf-8') as fileout:
			fileout.write(str(current))
		with open(objfile, mode='w', encoding='utf-8') as fileout:
			fileout.write(markercols)
			fileout.write(current.cols())
		with open(geofile, mode='w', encoding='utf-8') as fileout:
			fileout.write(str(current.Coordinate))

		markerout.write("{0}\n".format(current.cols()))

	except NameError as detail:
		print_err("Bad location entry: ", detail)
	except IOError as detail:
		print_err("Could not print data", detail)
	except ValueError as detail:
		print_err("Bad data value: ", detail)
		xml_root.remove(element)
	except:
		print_err("Unexpected error:", sys.exc_info()[0])
		raise

markerout.close()
full_tree.write(indexxml, encoding='utf-8', xml_declaration=True)
